package com.bone.eventsweb.controller;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bone.eventsweb.dao.NavigationDao;
import com.bone.eventsweb.utils.PageUtils;
import com.bone.eventsweb.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.bone.eventsweb.entity.NavigationEntity;
import com.bone.eventsweb.service.NavigationService;

/**
 * 
 *
 * @author dkx
 * @email null
 * @date 2024-02-26 15:06:22
 */
@RestController
@RequestMapping("/eventsweb/navigation")
public class NavigationController {
    @Autowired
    private NavigationService navigationService;
    @Autowired
    private NavigationDao navigationDao;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = navigationService.queryPage(params);
        return R.ok().put("page", page);
    }


    @GetMapping("/findAllNavigation")
    public  List<NavigationEntity> list(){
        List<NavigationEntity> list = navigationService.list(null);
        if (list!=null ){
            return list;
        }
        return null;
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		NavigationEntity navigation = navigationService.getById(id);
        return R.ok().put("navigation", navigation);
    }

    /**
     * 保存
     */
//    @PostMapping("/save")
//    public String save(@RequestBody NavigationEntity navigation){
//        System.out.println("==========="+navigation);
//        boolean flag = navigationService.save(navigation);
//        if (flag){
//            return "添加成功";
//        }
//        return "添加失败";
//    }
    @PostMapping("/save")
    public String save(@RequestBody NavigationEntity navigation){
        System.out.println("==========="+navigation);
        boolean flag = navigationService.save(navigation);
       return "";
    }


    @RequestMapping("/save1")
    public String save1(@RequestBody NavigationEntity navigation){
        navigationService.save(navigation);
        return navigation+"";
    }


    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody NavigationEntity navigation){

        System.out.println(navigation+"=============");
		navigationService.updateById(navigation);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		navigationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     * 删除
     */
    @RequestMapping("/removeById/{id}")
    public String removeById(@PathVariable("id") Integer id){
        System.out.println(id+"==================");
        boolean flag = navigationService.removeById(id);
       if (flag){
            return "删除成功";
        }
        return "删除失败";
    }


        @RequestMapping("/test")
        public void test() {
            // 模拟复杂分页查询
            //current 当前页   size 最大页数
            IPage<NavigationEntity> navigationEntityIPage = navigationDao.selectPage(new Page<>(1, 2), null);
            System.out.println("总页数： " + navigationEntityIPage.getPages());
            System.out.println("总记录数： " + navigationEntityIPage.getTotal());
            navigationEntityIPage.getRecords().forEach(System.out::println);
            // 简单分页查询
            QueryWrapper<NavigationEntity> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("title", title);
            navigationDao.selectPage(new Page<>(1, 2), queryWrapper);
            System.out.println("总页数： " + navigationEntityIPage.getPages());
            System.out.println("总记录数： " + navigationEntityIPage.getTotal());
            navigationEntityIPage.getRecords().forEach(System.out::println);
        }


        @GetMapping("/findByPage/{currentPage}/{pageSize}")
        public IPage findByPage( @PathVariable Integer currentPage,@PathVariable Integer pageSize){
            System.out.println("currentPage "+currentPage +" pageSize" + pageSize);
            //色孩子起始值每页条数
            Page<NavigationEntity> page = new Page<>(currentPage, pageSize);
            Page<NavigationEntity> iPage = navigationDao.selectPage(page, null);
            return iPage;
        }




}
